home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Need HELP by a C hacker....
- Date: 1 Mar 1996 13:31:56 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h7qccINN8m9@anvil.ugrad.cs.ubc.ca>
- References: <4h5mhv$mh9@newsstand.cit.cornell.edu>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4h5mhv$mh9@newsstand.cit.cornell.edu>,
- NOBODY <rezab@nova.npac.syr.edu> wrote:
- >Hello Everyone.
- >
- >I have a question:
- >
- >I want to declare a function
- >
- >
- >_____ foo(void){
- > return foo;
- > }
- >
- >Ususally _____ is the type of the thing that I am returning.
- >In this case it's a pointer to a function that returns a pointer
- >to a function. Does anyone have any ideas?
- >I have gone nuts trying to figure this one out.
- >BTW, I am using the gcc compiler (My microsoft compiler does not allow
- >anything like this to be declared).
-
- Your compiler is broken (assuming you have done your part correctly) if it will
- not take the declaration. It is valid for a function to return a pointer to a
- function, and it is most certain that the foo identifier is declared in the
- scope inside foo() itself---otherwise recursion with functions that return
- types other than int would not be possible without a prototype!
-
- What the compiler should be complaining about is that what you are returning is
- not compatible with the declared return type.
-
- Also, if you are trying to replace ______ with "void (*)(void)", which is a
- type expression for a pointer to a function returning void with no arguments,
- you are wrong. Such type expressions are only suitable in syntactic units like
- casts and sizeof(), and are not suitable in a declaration, where an explicit
- name is required to be infixed. It's a common beginner mistake to expect
- cast-form type expresions to be suitable in declarations.
-
- Try:
-
- void (* foo(void))(void)
-
- {
- return foo;
- }
-
- It states that foo is function which takes no arguments and which returns a
- pointer to a function which also has no arguments which returns a pointer to
- void.
-
- There doesn't appear to be a way to define foo such that it returns its own
- type, since the inner declaration (which declares a return type T) would have
- to contain a full embedding of the outer declaration (the function having
- arguments X returning T). I hope you realize that this is not possible without
- infinite regress.
-
- What is the point of returning a pointer to a function from the function
- itself? You can just write "foo" anywhere you need its value. If you can call
- foo, then you already know its address, so there is no point in computing it
- inside foo.
- --
-
-